package command

import (
	"ssadmin/common/constant"
	"ssadmin/internal/admin/models"
	"strings"
	"time"

	"gitee.com/sansaniot/ssiot-core/httpmvc/jwtauth/user"
	"github.com/gin-gonic/gin"
	uuid "github.com/satori/go.uuid"
)

type MenuOrderReq struct {
	MenuId   string `json:"id" comment:""`
	OrderNum int32  `json:"orderNum" comment:""`
	ParentId string `json:"parentId" comment:""`
}

type MenuInsertReq struct {
	MenuId     string    `json:"-" comment:""`
	ParentId   string    `json:"parentId" comment:""`
	MenuName   string    `json:"menuName" comment:""`
	Path       string    `json:"path" comment:""`
	Component  string    `json:"component" comment:""`
	Perms      string    `json:"perms" comment:""`
	Icon       string    `json:"icon" comment:""`
	Type       string    `json:"type" comment:""`
	OrderNum   int32     `json:"orderNum" comment:""`
	CreateTime time.Time `json:"createTime" comment:""`
	UpdateTime time.Time `json:"updateTime" comment:""`
	CreatorId  string    `json:"creatorId" comment:""`
	Creator    string    `json:"creator" comment:""`
	Title      string    `json:"title" comment:""`
	BtnClass   string    `json:"btnClass" comment:""`
	BtnGroup   string    `json:"btnGroup" comment:""`
	Enable     string    `json:"enable" comment:""`
	Lang       string    `json:"lang" comment:""`
	Systemid   string    `json:"systemid" comment:""`
	Alias      string    `json:"alias" comment:"别名"`
}

func (s *MenuInsertReq) Bind(c *gin.Context) {
	//获取扩展属性（自定义扩展）
	s.MenuId = uuid.NewV4().String()
	s.Systemid = c.GetHeader(constant.CacheKeySystemId)
	s.Creator = user.Get(c, constant.CacheKeyUserName).(string)
	s.CreatorId = user.Get(c, constant.CacheKeyUserId).(string)
	s.CreateTime = time.Now()
	s.UpdateTime = time.Now()
}

func (s *MenuInsertReq) Generate(model *models.Menu) {
	if len(s.MenuId) == 0 {
		model.MenuId = uuid.NewV4().String()
	} else {
		model.MenuId = s.MenuId
	}
	if len(s.ParentId) == 0 {
		s.ParentId = "0"
	}
	model.ParentId = s.ParentId
	model.MenuName = s.MenuName
	model.Path = s.Path
	model.Component = s.Component
	model.Perms = s.Perms
	model.Icon = s.Icon
	model.Type = s.Type
	model.OrderNum = s.OrderNum
	model.CreateTimeDts = time.Now()
	model.ModifyTimeDts = time.Now()
	model.CreatorId = s.CreatorId
	model.Creator = s.Creator
	model.Title = s.Title
	model.BtnClass = s.BtnClass
	model.BtnGroup = s.BtnGroup
	model.Enable = s.Enable
	model.Lang = s.Lang
	model.Systemid = s.Systemid
}

func (s *MenuInsertReq) GetId() interface{} {
	return s.MenuId
}

type MenuUpdateReq struct {
	MenuId     string    `json:"id"`
	ParentId   string    `json:"parentId" comment:""`
	MenuName   string    `json:"menuName" comment:""`
	Path       string    `json:"path" comment:""`
	Component  string    `json:"component" comment:""`
	Perms      string    `json:"perms" comment:""`
	Icon       string    `json:"icon" comment:""`
	Type       string    `json:"type" comment:""`
	OrderNum   int32     `json:"orderNum" comment:""`
	ModifyTime time.Time `json:"updateTime" comment:""`
	Creator    string    `json:"creator" comment:""`
	Level      int       `json:"level" comment:""`
	Title      string    `json:"title" comment:""`
	BtnClass   string    `json:"btnClass" comment:""`
	BtnGroup   string    `json:"btnGroup" comment:""`
	Enable     string    `json:"enable" comment:""`
	Lang       string    `json:"lang" comment:""`
	Systemid   string    `json:"systemid" comment:""`
	Alias      string    `json:"alias" comment:"别名"`
	UserInfo
}

func (s *MenuUpdateReq) Generate(model *models.Menu) {
	model.MenuName = s.MenuName
	model.Path = s.Path
	model.Component = s.Component
	model.Perms = s.Perms
	model.Icon = s.Icon
	model.Type = s.Type
	model.OrderNum = s.OrderNum
	model.ModifyTimeDts = time.Now()
	model.Title = s.Title
	model.BtnClass = s.BtnClass
	model.BtnGroup = s.BtnGroup
	model.Enable = s.Enable
	model.Lang = s.Lang
	model.ParentId = s.ParentId
	model.CreatorId = s.UserId
	model.Creator = s.UserName
}

func (s *MenuUpdateReq) GetId() interface{} {
	return s.MenuId
}

// MenuDeleteReq 功能删除请求参数
type MenuDeleteReq struct {
	Ids     string   `json:"ids"`
	MenuIds []string `json:"-" comment:""`
}

func (s *MenuDeleteReq) GetId() []string {
	s.MenuIds = strings.Split(s.Ids, ",")
	return s.MenuIds
}
